package org.fig.ioc.bean.definition;

import org.fig.ioc.bean.factory.DefaultUserFactory;
import org.fig.ioc.bean.factory.UserFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * @Description:
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/15 22:27
 */

@Configuration
public class BeanInitializationDemo {
    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(BeanInitializationDemo.class);
        //启动Spring应用上下文
        applicationContext.refresh();
        //非延迟初始化在Spring应用上下文启动后完成，被初始化
        System.out.println("Spring应用上下文已启动");

        //依赖查找UserFactory
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);
        System.out.println(userFactory);
        //关闭Spring应用上下文
        System.out.println("Spring应用上下文准备关闭");
        applicationContext.close();
        System.out.println("Spring应用上下文已关闭");
    }

/*    @Bean
    public UserFactory userFactory(){
        return new DefaultUserFactory();
    }*/

    @Bean(initMethod = "initUserFactory",destroyMethod = "initUserFactory")
    @Lazy(value = false)
    public UserFactory userFactory(){
        return new DefaultUserFactory();
    }




}
